﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using org.in2bits.MyXls;

using OgilvyOne.UKNOW.EntityDB;


using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;

public partial class cn_VicoQuery : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        { 
            Init();
            Query();
        }
    }
    /// <summary>
    /// 时间条件初始化
    /// </summary>
    private void Init()
    {
        MulDate1.InitDate();
        MulDate1.IsRange = true;
        MulDate1.IsMonth = false;
        MulDate1.IsWeek = false;
        MulDate1.IsDay = false;
        MulDate1.IsHourDdl = false;
        
    }
    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Btn_Query_Click(object sender, EventArgs e)
    {
        Query();

    }

    /// <summary>
    /// 导出
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Btn_Export_Click(object sender, EventArgs e)
    {

        DataTable dt = (DataTable)ViewState["regquery"];
        List<string> columnList = new List<string>();
        foreach (DataColumn dc in dt.Columns)
        {
            columnList.Add(dc.ColumnName);
        }
        string pathName = HttpContext.Current.Server.MapPath("excel/");
        string tableName = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");//获得上传时间
        ExcelSaves(dt, columnList.ToArray(), pathName, tableName);
        string path = pathName + tableName + ".xls";

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
        if (fileInfo.Exists == false)
        {
            Response.Write("<script>alert('当前无可用数据导出!');</script>");
           
            return;
        }
        Response.Clear();
        Response.ContentType = "application/x-excel";
        Response.AddHeader("Content-Disposition", "attachment;FileName=" + HttpUtility.UrlEncode(tableName + ".xls", System.Text.Encoding.UTF8));
        Response.WriteFile(path);
    }


    public static string ExcelSaves(System.Data.DataTable dt, string[] teRows, string pathName, string tableName)
    {

        if (teRows.Length == 0 || teRows == null || dt == null || dt.Rows.Count == 0 || pathName == "" || tableName == "")
        {

            return null;
        }
        if (teRows.Length != dt.Columns.Count)
        {
            return null;
        }
        string newPath = tableName + ".xls";



        try
        {
            XlsDocument doc = new XlsDocument();
            doc.FileName = newPath;

            Worksheet sheet = doc.Workbook.Worksheets.Add("Sheet1");
            Cell cell = null;
            try
            {
                //生成表头
                for (int i = 0; i < teRows.Length; i++)
                {
                    cell = sheet.Cells.Add(1, i + 1, teRows[i].ToString());
                }

                //生成表数据
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        if (dt.Rows[i][j] != null && dt.Rows[i][j].ToString() != "")
                        {
                            cell = sheet.Cells.Add(i + 2, j + 1, dt.Rows[i][j].ToString());

                        }
                    }
                }

            }
            catch (Exception ex)
            {
                return null;
                throw;

            }
            try
            {

                doc.Save(pathName);

            }
            catch (Exception ex)
            {

                return null;
                throw;
            }



        }
        catch (Exception ex)
        {
            return null;
            throw;
        }


        return newPath;
    }
    /// <summary>
    /// 绑定GridView
    /// </summary>
    private void Query()
    {
        DateTime startTime =MulDate1.StartDate;
        DateTime endTime = MulDate1.EndDate;
       // string userType = ddlType.SelectedItem.Value;
        if (startTime > endTime)
        {
            lblMsg.Text = "起始时间大于结束时间，请重新选择！";
            return;
        }
        else
        {
            lblMsg.Text = "";
        }

        string staDate = startTime.ToString("yyyy-MM-dd");
        string endDate = endTime.ToString("yyyy-MM-dd");
        DataTable dt = UserInfoDB.QueryByTime(startTime.ToString("yyyy-MM-dd"), endTime.ToString("yyyy-MM-dd"));
        GvReg.DataSource = dt;
        GvReg.DataBind();
        ViewState["regquery"] = dt;
    }

    protected void GvReg_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GvReg.PageIndex = e.NewPageIndex;
        DataTable dt = (DataTable)ViewState["regquery"];
        GvReg.DataSource = dt;
        GvReg.DataBind();
    }
    protected void GvReg_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.Header) || (e.Row.RowType == DataControlRowType.DataRow))
        {
            e.Row.Cells[0].Attributes.CssStyle.Add("display", "none");
        }
    }
}
